// OSR b finddir
package org.jvcs.utils;

import java.util.*;
import java.io.*;

import org.jvcs.common.*;
import org.jvcs.utils.*;

public final class Find {
 
  public static String sDir = ".";
  public static String sWildCard = "*";
  public static int findFlag = 0;
  public static int FIND_FILE = 1;
  public static int FIND_DIR = 2;
  public static int FIND_ALL = 3;
  public static List<String> foundItems = new ArrayList<String>();
  
   public static void findDir(String sDir1, String sWildCard1)  {
		sDir = sDir1;
		sWildCard = sWildCard1;
		sDir = Utils.stdPath(sDir);
		File sdx = new File(sDir);
		findFlag = FIND_DIR;
		getFileListing(sdx);
		for(String file : foundItems) {
			osr.pl("FINAL: "+file);
		}
   }
  
   public static void findFile(String sDir1, String sWildCard1) {
		sDir = sDir1;
		sWildCard = sWildCard1;
		sDir = Utils.stdPath(sDir);
		File sdx = new File(sDir);
		findFlag = FIND_FILE;
		getFileListing(sdx);
		for(String file : foundItems) {
			osr.pl("FINAL: "+file);
		}
	}
  
   public static void findAll(String sDir1, String sWildCard1) {
		sDir = sDir1;
		sWildCard = sWildCard1;
		sDir = Utils.stdPath(sDir);
		File sdx = new File(sDir);
		findFlag = FIND_ALL;
		getFileListing(sdx);
		for(String file : foundItems) {
			osr.pl("FINAL: "+file);
		}
   }
   
   static public void getFileListing(File aStartingDir) {
		validateDirectory(aStartingDir);
		List<File> result = getFileListingNoSort(aStartingDir);
		Collections.sort(result);
    }

  // PRIVATE //
  static private List<File> getFileListingNoSort(File aStartingDir) {
	List<File> result = new ArrayList<File>();
	String eText = "";

  try {
	
    File[] filesAndDirs = aStartingDir.listFiles();
	List<File> filesDirs = Arrays.asList(filesAndDirs);

    for(File file : filesDirs) {
      if (file == null) break;
	  if (CmdLine.verboseFlag) osr.pl("CHECKING: "+file.toString());
	  result.add(file);
      if ( ! file.isFile() ) {
		if ( (findFlag == FIND_DIR) || (findFlag == FIND_ALL)) {
			if (CmdLine.verboseFlag) osr.pl("Find Dir start ..."+file.toString());
			String regex = sWildCard.replace("?", ".?").replace(".", "\\.").replace("*", ".*?");
			String fStr = file.getName();
			String fDisp = file.toString();
			fDisp = Utils.stdPath(fDisp);
			if ((fStr.matches(regex))) {
				osr.pl(""+fDisp);
				foundItems.add(fDisp);
			}
		}
			List<File> deeperList = getFileListingNoSort(file);
			result.addAll(deeperList);
		

      } else {
		if ( (findFlag == FIND_FILE) || (findFlag == FIND_ALL)) {
			if (CmdLine.verboseFlag) osr.pl("Find File start ..."+file.toString());
			String regex = sWildCard.replace("?", ".?").replace(".", "\\.").replace("*", ".*?");
			String fStr = file.getName();
			String fDisp = file.toString();
			fDisp = Utils.stdPath(fDisp);
			if ((fStr.matches(regex))) {
				osr.pl(""+fDisp);
				foundItems.add(fDisp);
			}
		}
		
	  }
    }
	} catch (Exception e) {
		if (CmdLine.verboseFlag) osr.pl("Empty Dir Found");
	}
    return result;
  }

   
   static private void validateDirectory (File aDirectory) {
   try {
    if (aDirectory == null) {
      osr.ee("Directory should not be null.");
    }
    if (!aDirectory.exists()) {
      osr.ee("Directory does not exist: " + aDirectory);
    }
    if (!aDirectory.isDirectory()) {
      osr.ee("Is not a directory: " + aDirectory);
    }
    if (!aDirectory.canRead()) {
      osr.ee("Directory cannot be read: " + aDirectory);
    }
	} catch (Exception e) {
		osr.pl("validateDirectory "+e.toString());
	}
  }


 }